<?php

/**
 * plant actions.
 *
 * @package    seth
 * @subpackage plant
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class plantActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  { 
      $order=$request->getParameter('orderBy', 'pn');
      //Se cambio campo ordinante metto ASC, altrimenti scambio ASC e DESC
      $orderOption= $this->getUser()->SwitchOrderOption($order);
      $this->getUser()->setOrderField($order);
     
      $this->plants=Doctrine_Core::getTable('Plant')->GetPlantIndex(array(
          'orderBy'=>$order,
          'indexOrder'=>$orderOption));
   }
  
   public function executePowerdevIndex(sfWebRequest $request)
  {
       $this->plant = $this->getRoute()->getObject();
       $this->devs=$this->plant->getPowerdev();
       $recs=$this->plant->getPowerdevTypes();
       $this->devTypes=array();
       foreach($recs as $rec)
           array_push ($this->devTypes, $rec["type"]);
       
   }

  public function executeShow(sfWebRequest $request)
  {
    $this->plant = $this->getRoute()->getObject();
    $this->devs=$this->plant->getPowerdev();
    $recs=$this->plant->getPowerdevTypes();
    $this->devTypes=array();
    foreach($recs as $rec)
        array_push ($this->devTypes, $rec["type"]);
    
    $this->subPlants=$this->plant->getSubplant();
    $this->netDevs=$this->plant->getNetDev();
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PlantForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new PlantForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->form = new PlantForm($this->getRoute()->getObject());
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->form = new PlantForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->getRoute()->getObject()->delete();

    $this->redirect('plant/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $plant = $form->save();
      $this->redirect('plant/show?id='.$plant->getId());
    }
  }
  
}
